#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/thread_info.h>
 
static int __init print_stack_info_init(void)
{
    struct task_struct *task;
    struct mm_struct *mm;
    unsigned long user_stack_start, user_stack_end, user_stack_size;
    unsigned long kernel_stack_start, kernel_stack_end, kernel_stack_size;

    // 获取当前进程的 task_struct
    task = current;

    // 获取用户态堆��信息
    mm = task->mm;
    if (mm) {
        user_stack_start = mm->start_stack;
        user_stack_end = mm->start_stack - mm->stack_vm * PAGE_SIZE;
        user_stack_size = mm->stack_vm * PAGE_SIZE;
        printk(KERN_INFO "User Stack: Start=0x%lx, Size=%lu bytes\n", user_stack_start, user_stack_size);
    } else {
        printk(KERN_INFO "No user stack available.\n");
    }

    // 获取内核态堆栈信息
    kernel_stack_start = (unsigned long)task->stack;
    kernel_stack_end = kernel_stack_start + THREAD_SIZE;
    kernel_stack_size = THREAD_SIZE;
    printk(KERN_INFO "Kernel Stack: Start=0x%lx, Size=%lu bytes\n", kernel_stack_start, kernel_stack_size);

    return 0;
}

static void __exit print_stack_info_exit(void)
{
    printk(KERN_INFO "Module exit.\n");
}

module_init(print_stack_info_init);
module_exit(print_stack_info_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Print current task user and kernel stack info");
